package com.xy.blog.article.domain.service.impl.anchor;

import com.alibaba.fastjson.JSON;
import com.xy.blog.article.domain.service.IAnchorService;
import com.xy.blog.article.domain.service.model.AnchorBO;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description:
 * @date 2020/6/29
 */
@Service
public class HtmlAnchorServiceImpl implements IAnchorService {

    @Override
    public List<AnchorBO> generateAnchor(String contentHtml) {
        Map<Integer, AnchorBO> mapLevel = new HashMap<>(6);

        Document parse = Jsoup.parse(contentHtml);
        Element body = parse.body();
        Elements elementsByClass = body.getElementsByAttribute("id");
        List<AnchorBO> list = new ArrayList<>();
        Iterator<Element> iterator = elementsByClass.iterator();
        int preLevel = 1;
        while (iterator.hasNext()) {
            Element element = iterator.next();
            int tagLevel = Integer.parseInt(element.tagName().substring(1));
            String id = element.id();
            String text = element.text();
            AnchorBO anchorBO = new AnchorBO();
            anchorBO.setHref(id);
            anchorBO.setTitle(text);
            if (tagLevel > preLevel) {
                // 继续添加该深度
                int max = Math.max((list.size() - 1), 0);
                AnchorBO point = list.get(max);
                if (point == null) {
                    point = anchorBO;
                    list.add(point);
                    continue;
                }
                List<AnchorBO> children = new ArrayList<>();

                while (point.getChildren() != null) {
                    List<AnchorBO> childrenPoint = point.getChildren();
                    int size = childrenPoint.size();
                    point = childrenPoint.get(Math.max(size - 1, 0));
                }
                children.add(anchorBO);
                point.setChildren(children);
            } else  {
                // 同级 或者 重新创建一个新的链表
                list.add(anchorBO);
            }
            mapLevel.put(tagLevel, anchorBO);
        }
        return list;
    }


    /*
     *

     * |
     *   |
     *     |
     *   |
     *
     *
     *
     *
     *
     *
     *

     */

    public static void main(String[] args) {
        String str = "<h1 id=\"d2na2\">adfsadfasdf</h1><p></p>" +
                "<h2 id=\"1nid8\">sdfasdfa</h2><p></p>" +
                "<h2 id=\"1n2d8\">sdfasdfa</h2><p></p>" +
                "<h3 id=\"csj2a\">asdfasdf</h3><p></p><p></p>" +
                "<h3 id=\"cs32a\">asdfasdf</h3><p></p><p></p>" +
                "<h2 id=\"68nl3\">safasdfasf</h4><p></p>" +
                "<h3 id=\"a9q23\">sfasdfasdfasdf</h6>\n";
        HtmlAnchorServiceImpl htmlAnchorService = new HtmlAnchorServiceImpl();
        List<AnchorBO> anchorBOS = htmlAnchorService.generateAnchor(str);
        System.out.println(JSON.toJSONString(anchorBOS));
    }
}
